package com.xju.admin.controller;

import com.xju.admin.entity.Exam;
import com.xju.admin.entity.ExamResult;
import com.xju.admin.entity.ExamShow;
import com.xju.admin.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

@Controller
public class ExamController {

    @Autowired
    private ExamService examService;

    @RequestMapping("exam-list")
    public String examList(Model model){
        ExamResult examList=examService.findAll();
        model.addAttribute("examList",examList);
        return "module_one/exam_list";
    }

    @RequestMapping("exam-delete")
    @ResponseBody
    public Object examDelete(@RequestParam(value = "id") String id){
        examService.delete(id);
        Map<String,Object> map=new HashMap<>();
        map.put("ok",true);
        return map;
    }

    @RequestMapping("download-paper")
    public void downloadPaper(HttpServletRequest request, HttpServletResponse response, String path) {
        File file = new File(path);
        String filename = file.getName();
        if (file.exists()) { //判断文件父目录是否存在
//            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/force-download");
            try {
                response.setHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode(filename, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            byte[] buffer = new byte[1024];
            FileInputStream fis = null; //文件输入流
            BufferedInputStream bis = null;

            OutputStream os = null; //输出流
            try {
                os = response.getOutputStream();
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                int i = bis.read(buffer);
                while (i != -1) {
                    os.write(buffer);
                    i = bis.read(buffer);
                }

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                bis.close();
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("exam-review-page")
    public String reviewPage(Model model){
        ExamResult examResult=examService.review();
        model.addAttribute("examList",examResult);
        return "module_one/exam_review";
    }

    @RequestMapping("exam-pass")
    @ResponseBody
    public Object pass(String id){
        examService.pass(id);
        Map<String,Object> map= new HashMap<>();
        map.put("ok",true);
        return map;
    }
}
